<?php

namespace App\Http\Controllers\OA;

use App\Http\Controllers\Controller;
use App\Model\OACurrency;
use Illuminate\Http\Request;

class HomeController extends Controller
{
    public function index()
    {
        //$this->test();
        //将字符串写进本地文件
        $path = storage_path('json/html.txt');
        if(!file_exists($path)){
            touch($path);
            $url = 'http://hl.anseo.cn/';
            $html = file_get_contents($url);//下载文件并保存本地
            file_put_contents($path,$html);
            echo 'success';
        }else{
            $html = file_get_contents($path);
            $data_ev = $this->get_tag_data($html,'li','class','ev');
            $data_od = $this->get_tag_data($html,'li','class','od');
            //dd($data_ev);
            $data = [];
            foreach ($data_ev as $str) {
               $data['ev'][] = $this->getRate($str);
               //dd($this->getRate($str));
            }
            foreach ($data_od as $str){
                try{
                    $data['od'][] = $this->getRate($str);
                }catch(\Exception $e){
                    $data['od']['err'][] = $str;
                }

            }
            unset($data['od']['err']);

            $res = $this->insertData($data);
            dump($res);
        }

    }

    //提取html
    protected function get_tag_data($html,$tag,$class,$value){
        //$value 为空，则获取class=$class的所有内容
        $regex = $value ? "/<$tag.*?$class=\"$value\".*?>(.*?)<\/$tag>/is" :  "/<$tag.*?$class=\".*?$value.*?\".*?>(.*?)<\/$tag>/is";
        preg_match_all($regex,$html,$matches,PREG_PATTERN_ORDER);
        return $matches[1];//返回值为数组 ,查找到的标签内的内容
    }

    //提取汇率
    protected function getRate($str)
    {
        //$str = strip_tags($str);
        $ex1 = '/(\d+)\.(\d+)/is';
        $ex2 = "/(?:\()(.*)(?:\))/is";
        $ex3 = '/<a .*?href="(.*?)".*?>/is';
        //提取币种,汇率
        preg_match_all($ex1,$str,$arr1);
        preg_match_all($ex2,strip_tags($str),$arr2);
        preg_match_all($ex3,$str,$arr3);
        $rate = 0;
        $currency = 'RMB';
        $mark = '';
        if(isset($arr1) && !empty($arr1)) $rate = $arr1[0][0];
        if(isset($arr2) && !empty($arr2)) $currency = $arr2[1][0];
        if(isset($arr3) && !empty($arr3)) {
            $mark = $arr3[1][1];
            $mark = pathinfo($mark,PATHINFO_FILENAME);
            $mark = strtoupper(str_replace('cal_','',$mark));
            $mark_arr = explode('_',$mark);
            $from = $mark_arr[0];
            $to = $mark_arr[2];
        }

        return [
            'rate'=>$rate,
            'currency'=>$currency,
            'from'=>$from,
            'to'=>$to,
            'created_at'=>date('Y-m-d H:i:s'),
            'updated_at'=>date('Y-m-d H:i:s'),
        ];
    }

    //插入数据
    protected function insertData($data)
    {
        $insertData =  array_merge($data['ev'],$data['od']);
        //return $insertData;
        $model = new OACurrency();
        if(count($model::all()) > 0) return false;
        $model->insert($insertData);
        return true;
    }
}
